package com.vogue.supplymall.shop.controller;

import com.vogue.supplymall.common.constant.ErrorCode;
import com.vogue.supplymall.common.domain.dto.ControllerResultInfo;
import com.vogue.supplymall.common.domain.dto.RelevanceInfo;
import com.vogue.supplymall.common.domain.dto.UserInfo;
import com.vogue.supplymall.common.exception.DataNotFindException;
import com.vogue.supplymall.common.exception.ParamErrorException;
import com.vogue.supplymall.shop.common.CheckRelevanceUtils;
import com.vogue.supplymall.shop.domain.model.Relevance;
import com.vogue.supplymall.shop.domain.service.RelevanceService;
import com.vogue.supplymall.shop.exception.AddRelevanceException;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


import javax.annotation.Resource;

/**
 * The type Relevance controller.
 *
 * @aouthor: zhangliang
 * @date: Create in 2018-03-28
 */
@RestController
public class RelevanceController extends BaseController {
    /**
     * The constant LOGGER_RELEVANCECONTROLLER.
     */
    public static final Logger LOGGER_RELEVANCECONTROLLER = LoggerFactory.getLogger(RelevanceController.class);

    @Resource
    private RelevanceService relevanceService;

    /**
     * Add product watch response entity.
     *
     * @param appver        the appver
     * @param relevanceInfo the relevance info
     * @return the response entity
     */
    @RequestMapping(value = "/is/{appver}/product/watch/add", method = RequestMethod.POST)
    public ResponseEntity<Object> addProductWatch(@PathVariable("appver") String appver, @RequestBody RelevanceInfo relevanceInfo) {
        LOGGER_RELEVANCECONTROLLER.info(">>>>>> addProductWatch start");
        if (SecurityUtils.getSubject().isAuthenticated()) {
            UserInfo info = (UserInfo) SecurityUtils.getSubject().getPrincipal();
            relevanceInfo.setUserId(info.getLoginId());
            if (CheckRelevanceUtils.checkParamEmpty(relevanceInfo)
                    || 1 != relevanceInfo.getType()
                    || 2 != relevanceInfo.getTargetObjType()) {
                LOGGER_RELEVANCECONTROLLER.info("addProductWatch 关注商品参数不正确！");
                throw new ParamErrorException("");
            }
        } else {
            LOGGER_RELEVANCECONTROLLER.info("未登录");
        }

        Relevance relevance = null;
        try {
            relevance = relevanceService.addRelevance(relevanceInfo);
        } catch (Exception e) {
            throw new AddRelevanceException(ErrorCode.AddWatchFailure);
        }

        ControllerResultInfo resultInfo = new ControllerResultInfo();
        resultInfo.setResult(true);
        resultInfo.setData(relevance.getId());
        LOGGER_RELEVANCECONTROLLER.info(" <<<<<< addProductWatch end");
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
    }

    /**
     * Product watch list response entity.
     *
     * @param appver        the appver
     * @param relevanceInfo the relevance info
     * @return the response entity
     */
    @RequestMapping(value = "/is/{appver}/product/watch/list", method = RequestMethod.POST)
    public ResponseEntity<Object> productWatchList(@PathVariable("appver") String appver, @RequestBody RelevanceInfo relevanceInfo) {
        LOGGER_RELEVANCECONTROLLER.info(">>>>>> productWatchList start");
        if (SecurityUtils.getSubject().isAuthenticated()) {
            UserInfo info = (UserInfo) SecurityUtils.getSubject().getPrincipal();
            relevanceInfo.setUserId(info.getLoginId());
            if (CheckRelevanceUtils.checkProductWatchParam(relevanceInfo) || 2 != relevanceInfo.getTargetObjType()) {
                LOGGER_RELEVANCECONTROLLER.info("addProductWatch 关注商品参数不正确！");
                throw new ParamErrorException("");
            }
        } else {
            LOGGER_RELEVANCECONTROLLER.info("未登录");
        }


        ControllerResultInfo resultInfo = new ControllerResultInfo();
        resultInfo.setResult(true);
        resultInfo.setData(relevanceService.searchProductWatch(relevanceInfo));
        LOGGER_RELEVANCECONTROLLER.info(" <<<<<< productWatchList end");
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
    }

    /**
     * Add shop collect response entity.
     *
     * @param appver        the appver
     * @param relevanceInfo the relevance info
     * @return the response entity
     */
    @RequestMapping(value = "/is/{appver}/shop/watch/add", method = RequestMethod.POST)
    public ResponseEntity<Object> addShopWatch(@PathVariable("appver") String appver, @RequestBody RelevanceInfo relevanceInfo) {
        LOGGER_RELEVANCECONTROLLER.info(">>>>>> addShopWatch start");
        if (SecurityUtils.getSubject().isAuthenticated()) {
            UserInfo info = (UserInfo) SecurityUtils.getSubject().getPrincipal();
            relevanceInfo.setUserId(info.getLoginId());
            if (CheckRelevanceUtils.checkParamEmpty(relevanceInfo)
                    || 1 != relevanceInfo.getType()
                    || 3 != relevanceInfo.getTargetObjType()) {
                LOGGER_RELEVANCECONTROLLER.info("addShopWatch 关注店铺参数不正确！");
                throw new ParamErrorException("");
            }
        } else {
            LOGGER_RELEVANCECONTROLLER.info("未登录");
        }

        Relevance relevance = null;
        try {
            relevance = relevanceService.addRelevance(relevanceInfo);
        } catch (Exception e) {
            throw new AddRelevanceException(ErrorCode.AddWatchFailure);
        }

        ControllerResultInfo resultInfo = new ControllerResultInfo();
        resultInfo.setResult(true);
        resultInfo.setData(relevance.getId());
        LOGGER_RELEVANCECONTROLLER.info(" <<<<<< addShopWatch end");
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
    }

    /**
     * Shop collect list response entity.
     *
     * @param appver        the appver
     * @param relevanceInfo the relevance info
     * @return the response entity
     */
    @RequestMapping(value = "/is/{appver}/shop/watch/list", method = RequestMethod.POST)
    public ResponseEntity<Object> shopWatchList(@PathVariable("appver") String appver, @RequestBody RelevanceInfo relevanceInfo) {
        LOGGER_RELEVANCECONTROLLER.info(">>>>>> shopWatchList start");
        if (SecurityUtils.getSubject().isAuthenticated()) {
            UserInfo info = (UserInfo) SecurityUtils.getSubject().getPrincipal();
            relevanceInfo.setUserId(info.getLoginId());
            if (CheckRelevanceUtils.checkProductWatchParam(relevanceInfo) || 3 != relevanceInfo.getTargetObjType()) {
                LOGGER_RELEVANCECONTROLLER.info("shopWatchList 检索关注店铺参数不正确！");
                throw new ParamErrorException("");
            }
        } else {
            LOGGER_RELEVANCECONTROLLER.info("未登录");
        }

        ControllerResultInfo resultInfo = new ControllerResultInfo();
        resultInfo.setResult(true);
        resultInfo.setData(relevanceService.searchShopWatch(relevanceInfo));
        LOGGER_RELEVANCECONTROLLER.info(" <<<<<< shopWatchList end");
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
    }


    @RequestMapping(value = "/is/{appver}/shop/watch/statistics", method = RequestMethod.POST)
    public ResponseEntity<Object> getCount(@PathVariable("appver") String appver) {
        LOGGER_RELEVANCECONTROLLER.info(">>>>>> getCount start");
        RelevanceInfo relevanceInfo = new RelevanceInfo();
        if (SecurityUtils.getSubject().isAuthenticated()) {
            UserInfo info = (UserInfo) SecurityUtils.getSubject().getPrincipal();
            relevanceInfo.setUserId(info.getLoginId());
            if (null != info.getOrganInfo()) {
                relevanceInfo.setOrganid(info.getOrganInfo().getId());
            } else {
                relevanceInfo.setOrganid(info.getLoginId());
            }
            relevanceInfo.setSyscodeid(info.getSyscodeid());
        } else {
            LOGGER_RELEVANCECONTROLLER.info("未登录");
        }

        ControllerResultInfo resultInfo = new ControllerResultInfo();
        resultInfo.setResult(true);
        resultInfo.setData(relevanceService.countRelevance(relevanceInfo));
        LOGGER_RELEVANCECONTROLLER.info(" <<<<<< getCount end");
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
    }


    @RequestMapping(value = "/is/{appver}/shop/watch/remove/{id}", method = RequestMethod.POST)
    public ResponseEntity<Object> removeWatch(@PathVariable("appver") String appver, @PathVariable("id") Long id) {
        LOGGER_RELEVANCECONTROLLER.info(">>>>>> removeWatch start");
        RelevanceInfo relevanceInfo = new RelevanceInfo();
        if (SecurityUtils.getSubject().isAuthenticated()) {
            UserInfo info = (UserInfo) SecurityUtils.getSubject().getPrincipal();
            relevanceInfo.setUserId(info.getLoginId());
        } else {
            LOGGER_RELEVANCECONTROLLER.info("未登录");
        }

        if (null == id) {
            throw new DataNotFindException("");
        }

        relevanceInfo.setId(id);
        ControllerResultInfo resultInfo = new ControllerResultInfo();
        resultInfo.setResult(relevanceService.removeWatch(relevanceInfo));
        resultInfo.setData(null);
        LOGGER_RELEVANCECONTROLLER.info(" <<<<<< removeWatch end");
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
    }

    @RequestMapping(value = "/is/{appver}/shop/watch/remove", method = RequestMethod.POST)
    public ResponseEntity<Object> removeWatchs(@PathVariable("appver") String appver, @RequestBody RelevanceInfo relevanceInfo) {
        LOGGER_RELEVANCECONTROLLER.info(">>>>>> removeWatch start");
        if (SecurityUtils.getSubject().isAuthenticated()) {
            UserInfo info = (UserInfo) SecurityUtils.getSubject().getPrincipal();
            relevanceInfo.setUserId(info.getLoginId());
        } else {
            LOGGER_RELEVANCECONTROLLER.info("未登录");
        }

        if (null == relevanceInfo.getIds()) {
            throw new DataNotFindException("");
        }

        ControllerResultInfo resultInfo = new ControllerResultInfo();
        resultInfo.setResult(relevanceService.removeWatchs(relevanceInfo));
        resultInfo.setData(null);
        LOGGER_RELEVANCECONTROLLER.info(" <<<<<< removeWatch end");
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
    }
}
